<?php
declare (strict_types = 1);

namespace app\model;
use think\Exception;
use think\Model;

/**
 * 客户标签模型
 * @mixin Model
 */
class Category extends Model
{

    /**
     * @throws Exception
     */
    public function onBeforeInsert($model): void
   {
       $oid = request()->header('organization');
       if (!$oid){
          throw new Exception('组织ID不能为空，请创建或是加入一个组织');
       }
       $counts = $this->where('oid', $oid)
           ->where('type',$model['type'])
           ->count();
       if ($counts >= 50){
           throw new Exception('一个组织相同模块最多只能创建50个分类');
       }
       //自动设置组织ID
       $model['oid'] = $oid;
   }
}